package com.scalar.chiptrack.adaptor.ikanos.tsmc;

import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.tsmc.TSMCHtmlObserver;
import com.scalar.chiptrack.adaptor.tsmc.TSMCHtmlParser;
import com.scalar.chiptrack.adaptor.tsmc.TSMCLotStatus;
import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.users.VendorAdaptor;
import com.scalar.chiptrack.users.dao.AdaptorSetUpDAO;
import com.scalar.ScalarException;

import HTTPClient.HTTPConnection;
import HTTPClient.NVPair;
import HTTPClient.CookieModule;
import HTTPClient.HTTPResponse;
import HTTPClient.Cookie;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.Calendar;
import java.util.ArrayList;
import java.net.URL;

import Acme.HtmlScanner;

/**
 * Created by ChandraSekhar.
 * Date: June 06, 2007
 * Time: 11:48:39 AM
 */
public class TSMCAdaptor extends JobHandler implements AdaptorConstants
{
    private static final String URI_LOG_IN = "/online/login.do";
    private static final String URI_LOG_OUT = "/online/logout.do";
    private static final String URI_SHIPPING_INFO_QUERY = "/online/queryGen.do";
    private static final String URI_SHIPPING_INFO = "/online/queryGenZZPIResult.do";
    public static final String ORDERVSLOT_FILE_NAME = "ORDER_VS_LOTSTATUS_REPORT";
    public static final String SHIPPING_FILE_NAME = "SHIPPING_INFO";
    public static final String BINING_FILE_NAME = "BIN_INFO";

    private static final String CATEGORY_SHIPPING_INFO_QUERY = "-9933";
    private static final String CATEGORY_PARENT_SHIP_INFO_QUERY = "-9909";
    private static final String CATEGORY_ACTION_SHIP_INFO_QUERY = "init";
    public static final String CATEGORY_PCM_IQC_CP_RESULTS = "-9912";
    private static final String PARAM_VALUE_TRUE = "true";

    private static final String HOST_NAME = "ectonline.tsmc.com";
    private static final String PCM_HOST = "dst.tsmc.com";
    private static final int PORT_NO = 80;
    public static final String ADAPTOR_NAME = "TSMC_FAB";

    private Logger m_adaptorLogger = null;
    private String m_dataOutputDir = null;
    private String m_username = null;
    private String m_password = null;
    private String m_accountNumber = null;
    private int    m_adaptor_id     = -1;
    /**
     * - Called by HTML Scanner whenever it reads HTML pages
     * - Keeps track of required URLs
     */
    private TSMCHtmlObserver m_observer = null;
    private static URL s_dummyUrl = null;


    public TSMCAdaptor() throws Exception
    {

        if ( s_dummyUrl == null )
        {
            try
            {
                s_dummyUrl = new URL( "http://" + HOST_NAME );

            }
            catch ( Exception ignore )
            {
                throw ignore;
            }
        }

        try
        {
            VendorAdaptor vendorAdaptor = AdaptorSetUpDAO.getVendorAdaptorDetailsByVendorNameAndLocation( VENDOR_NAME_TSMC, LOCATION_TYPE_FAB );

            m_username = vendorAdaptor.getUserId().trim();
            m_password = vendorAdaptor.getPassWord().trim();
            m_accountNumber = vendorAdaptor.getCustomerId().trim();
            m_dataOutputDir = vendorAdaptor.getOutPutDirectory().trim();

            if ( m_dataOutputDir != null )
            {
                if ( m_dataOutputDir.endsWith( "/" ) || m_dataOutputDir.endsWith( "\\" ) )
                {
                    m_dataOutputDir = m_dataOutputDir + m_username;
                }
                else
                {
                    m_dataOutputDir = m_dataOutputDir + "/" + m_username;
                }

                AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
                m_adaptorLogger  =  AdaptorUtils.getAdaptorLogger( m_dataOutputDir, VENDOR_NAME_TSMC );
                AdaptorUtils.setLogger( m_adaptorLogger );

            }
        }
        catch ( ScalarException se )
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading TSMC Adaptor Username, Password details from database, Exception: " + se );
            throw se;
        }

        m_observer = new TSMCHtmlObserver();
    }


    public static void main( String[] args ) throws Exception
    {
        new TSMCAdaptor().execute();
    }


    public void execute() throws Exception
    {

        try
        {
            m_adaptor_id = AdaptorRunInfo.saveAdaptorLastUpdatedStatus( CONSTANT_ADAPTOR, ADAPTOR_NAME, CONSTANT_SUCCESS, -1);

            //To prints the Version Information
            AdaptorUtils.printHeader(VENDOR_NAME_TSMC, CONSTANT_ADAPTOR );

            //Removes the Old Records
            AdaptorUtils.removeOldFiles( m_dataOutputDir, m_adaptorLogger );

            // Login to TSMC Site
            HTTPConnection connection = login( HOST_NAME, PORT_NO, m_username, m_password );

            if ( connection != null )
            {
                // ---- Retrieve & Save Real Time Wip Information ----
                /*HTTPResponse rsp = requestRealTimeWipInfo( con );
                String realTimeWipFile = AdaptorUtils.saveAsFile( getReader( rsp ), m_dataOutputDir, ORDERVSLOT_FILE_NAME, AdaptorUtils.FILE_EXTN_CSV, true *//* append time stamp *//* );

                System.out.println( "\nReal Timp Wip information has been downloaded and saved as " + realTimeWipFile );*/

                HTTPResponse rsp = requestOrderVsLotStatusInfo( connection );
                if ( rsp != null )
                {
                    String lotOrderFile = AdaptorUtils.saveAsFile( getReader( rsp ), m_dataOutputDir, ORDERVSLOT_FILE_NAME, FILE_EXTN_CSV, true /* append time stamp */ );

                    System.out.println( "\nOrder Vs. Lot Status information has been downloaded and saved as " + lotOrderFile );
                    if ( m_adaptor_id > 0 )
                    {
                        AdaptorRunInfo.saveAdaptorFileInfo( m_adaptor_id , ORDERVSLOT_FILE_NAME, FILE_EXTN_CSV, StringUtils.formatDateandTime(DateUtil.getFormattedTimeStamp() )) ;
                    }
                    m_observer.setDownloadCsvURL( null );
                }
                else
                {
                    log( "Could not create response object of Order Vs. Lot Status" );
                    throw new ScalarException( "Could not create response object of Order Vs. Lot Status");
                }

                // ---- Retrieve & Save Shipping Status Information ----
                rsp = requestShippingInfo( connection );
                if ( rsp != null )
                {
                    String shippingInfoFile = AdaptorUtils.saveAsFile( getReader( rsp ), m_dataOutputDir, SHIPPING_FILE_NAME, AdaptorUtils.FILE_EXTN_CSV , true /* append time stamp */ );
                    System.out.println( "\nShipping information has been downloaded and saved as " + shippingInfoFile );
                    if ( m_adaptor_id > 0 )
                    {
                        AdaptorRunInfo.saveAdaptorFileInfo( m_adaptor_id , SHIPPING_FILE_NAME, FILE_EXTN_CSV, StringUtils.formatDateandTime(DateUtil.getFormattedTimeStamp() )) ;
                    }
                    m_observer.setDownloadCsvURL( null );
                }
                else
                {
                    log( "Could not create response object of Shipping Info..." );
                    throw new ScalarException( "Could not create response object of  Shipping Info...." );

                }

                requestSortBinInfoData (connection );
                
                // Logout from TSMC Site
                logout( connection );
            }
            else
            {
                log( "Could not able to get the Connection to TSMC web site" );
                throw new ScalarException( "Could not able to get the Connection to TSMC web site" );
            }
        }
        catch (Exception e )
        {
            AdaptorUtils.removeOldFiles( m_dataOutputDir, m_adaptorLogger);
            AdaptorRunInfo.updatedAdaptorStatus( CONSTANT_FAILURE, m_adaptor_id );
            System.out.println("Exception while trying to connect and read data from "+ HOST_NAME +" site: " + e);
            e.printStackTrace();
            throw e;
        }

    }


    /**
     * Logs into the TSMC site
     *
     * @param host TSMC Host Name
     * @param port TSMC Port
     * @param login user login
     * @param password user password
     *
     * @return HTTPConnection connection on which all other requests are sent
     * @throws Exception If there are any problems with login
     */
    private HTTPConnection login( String host, int port, String login, String password ) throws Exception
    {
        byte[] data = new byte[1024];

        NVPair defaultHeaders[] = new NVPair[11];
        defaultHeaders[0] = new NVPair( "Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, */*" );
        defaultHeaders[1] = new NVPair( "Referer", "http://" + host );
        defaultHeaders[2] = new NVPair( "Accept-Language", "en-us" );
        defaultHeaders[3] = new NVPair( "Content-Type", "application/x-www-form-urlencoded" );
        defaultHeaders[4] = new NVPair( "Accept-Encoding", "gzip, deflate" );
        defaultHeaders[5] = new NVPair( "Proxy-Connection", "Keep-Alive" );
        defaultHeaders[6] = new NVPair( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" );
        defaultHeaders[7] = new NVPair( "Host", host );
        defaultHeaders[8] = new NVPair( "Pragma", "no-cache" );
        defaultHeaders[9] = new NVPair( "Cookie", "name=Online" );
        defaultHeaders[10] = new NVPair( "Content-Length", "66" );

        NVPair[] formData = new NVPair[4];
        formData[0] = new NVPair( "userAlias", login );
        formData[1] = new NVPair( "userPassword", password );
        formData[2] = new NVPair( "tcount", "1" );
        formData[3] = new NVPair( "edit_userpass_value", "Enter" );
        //HTTPConnection.setProxyServer( "scalar2" , 5000  );
        HTTPConnection con = new HTTPConnection( host, port );
        con.setDefaultHeaders( defaultHeaders );

        // Accept all cookies
        CookieModule.setCookiePolicyHandler( null );
        con.addDefaultModule( CookieModule.class, -1 );

        System.out.print( "\nTrying to log in... " );
        HTTPResponse rsp = con.Post( URI_LOG_IN, formData, defaultHeaders );
       /* while ( rsp.getInputStream().read( data ) != -1 )
        {
            //System.out.println(new String(data));
        }*/

        if ( rsp.getEffectiveURI().toString().indexOf("/login.do") != -1 || rsp.getEffectiveURI().toString().indexOf("/changePwd.do") != -1 )
        {
            throw new ScalarException("********ERROR!! ERROR!! ERROR!! InValid Password, Please Reset**********\n\n ");
        }
        System.out.println( "\nsuccessfully logged in" );


        Cookie[] cookie = CookieModule.listAllCookies();

        for ( int i = 0; i < cookie.length; i++ )
        {
           //System.out.println("i="+i+" Name = "+cookie[i].getName()+" Value = "+cookie[i].getValue());
            if ( cookie[i].getName().trim().equalsIgnoreCase( "ON_SESS" ) )
            {
                m_observer.setBvSessionId( cookie[i].getValue() );
            }
            if ( cookie[i].getName().trim().equalsIgnoreCase( "ON_ENG_TW" ) )
            {
                m_observer.setBvEngineId( cookie[i].getValue() );
            }
            if ( cookie[i].getName().trim().equalsIgnoreCase( "ON_USER_ID" ) )
            {                   
                m_observer.setOnUserId( cookie[i].getValue() );
            }
        }
        /*System.out.print( "Passing thru intermediate screen - I ..." );

        formData[0] = new NVPair( "BV_SessionID", m_observer.getBvSessionId() );
        formData[1] = null;
        formData[2] = null;

        rsp = con.Get( "/online/tsmcOnlineAction.do", formData );*/


        return con;
    }

    /**
     * Sends HTTP Post to get Real Time Wip Information
     *
     * @param con connection on which the request is sent for Product Information
     * @return BufferedReader From which the fab data can be read
     *
     * @throws Exception If there is any problem Reading the web pages
     */
    private HTTPResponse requestRealTimeWipInfo( HTTPConnection con ) throws Exception
    {

        byte[] data = new byte[1024];


        // Real Time Wip - Screen 1

        HTTPResponse rsp = con.Get( "/online/ThirdPage.do?BV_SessionID="+ m_observer.getBvSessionId()+"&pmCatOID=-13342&parentOID=-9905&fid=realTime&turnKeyOID=-11742" );

        /*while ( rsp.getInputStream().read( data ) != -1 )
        {
            //System.out.println(new String(data));
        }*/

        // Real Time Wip - Right frame
        //System.out.print ("Real Time Wip HTML Form screen...");
        String URI_LOT_VS_LOT_STATUS_RT_FRAME = "/online/queryGen.do?BV_SessionID=" + m_observer.getBvSessionId() + "&pmCatOID=--13342&parentOID=-9905&action=init&pagingable=true";
        rsp = con.Get( URI_LOT_VS_LOT_STATUS_RT_FRAME );
        TSMCHtmlParser parser = new TSMCHtmlParser();

        parser.parse( new InputStreamReader( rsp.getInputStream() ) );

        System.out.println( "\nSubmitting the request for Real Time Wip Information ... " );
        NVPair[] formData = new NVPair[39];

        formData[0]     = new NVPair( "org.apache.struts.taglib.html.TOKEN", parser.getToken() );
        formData[1]     = new NVPair( "reportSetCd", "" );
        formData[2]     = new NVPair( "fid", "realTime" );
        formData[3]     = new NVPair( "BV_EngineID",  m_observer.getBvEngineId() );
        formData[4]     = new NVPair( "BV_SessionID", m_observer.getBvSessionId() );
        formData[5]     = new NVPair( "pmCatOID", "-13342" );
        formData[6]     = new NVPair( "turnKeyOID", "-11742" );
        formData[7]     = new NVPair( "pagingSize", "20" );
        formData[8]     = new NVPair( "surveyID", "21" );
        formData[9]     = new NVPair( "isSurveyed", "true" );
        formData[10]    = new NVPair( "pmFirstTime", "" );
        formData[11]    = new NVPair( "command", "query" );
        formData[12]    = new NVPair( "reportSetName", "" );
        formData[13]    = new NVPair( "isCustomized", "Y" );
        formData[14]    = new NVPair( "linkDesc", "Real Time WIP" );
        formData[15]    = new NVPair( "url", "/online/queryGen.do" );
        formData[16]    = new NVPair( "pageView", "true" );
        formData[17]    = new NVPair( "criterion0", "" );
        formData[18]    = new NVPair( "pmSeq", "LIST" );
        formData[19]    = new NVPair( "criterias", "Customer Product" );
        formData[20]    = new NVPair( "criterion1", "" );
        formData[21]    = new NVPair( "pmSeq", "LIST" );
        formData[22]    = new NVPair( "criterias", "TSMC Product" );
        formData[23]    = new NVPair( "criterion2", "" );
        formData[24]    = new NVPair( "pmSeq", "LIST" );
        formData[25]    = new NVPair( "criterias", "TSMC Lot ID" );
        formData[26]    = new NVPair( "criterion3",  m_accountNumber );
        formData[27]    = new NVPair( "pmSeq", "LIST" );
        formData[28]    = new NVPair( "criterias", "Account" );
        formData[29]    = new NVPair( "criterion4", "" );
        formData[30]    = new NVPair( "pmSeq", "LIST" );
        formData[31]    = new NVPair( "criterias", "Condition Filter" );
        formData[32]    = new NVPair( "criterion5", "" );
        formData[33]    = new NVPair( "pmSeq", "LIST" );
        formData[34]    = new NVPair( "criterias", "Customer Lot ID" );
        formData[35]    = new NVPair( "pagingable", "false" );
        formData[36]    = new NVPair( "pmRet", "/online/querygen.do" );
        formData[37]    = new NVPair( "onsubmit", "Submit" );
        formData[38]    = new NVPair( "HIDDEN_FORM", "true" );

        rsp = con.Post( "/online/queryGenZZPIResult.do", formData );

        return rsp;
    }

    /**
     * Sends HTTP Post to get Lot Vs Order Information
     *
     * @param con connection on which the request is sent for Product Information
     * @return BufferedReader From which the fab data can be read
     *
     * @throws Exception If there is any problem Reading the web pages
     */
    private HTTPResponse requestOrderVsLotStatusInfo( HTTPConnection con ) throws Exception
    {

        byte[] data = new byte[1024];

        // Lot Vs Order - Screen 1
        //System.out.print( "Passing thru Order Vs. Lot Status intermediate screen..." );
        String URI_LOT_VS_LOT_STATUS_SCREEN_1 = "/online/ThirdPage.do?BV_SessionID=" + m_observer.getBvSessionId() + "&pmCatOID=-9927&parentOID=-9905&action=init&pagingable=true";
        HTTPResponse rsp = con.Get( URI_LOT_VS_LOT_STATUS_SCREEN_1 );

        /*while ( rsp.getInputStream().read( data ) != -1 )
        {
            //System.out.println(new String(data));
        }*/
        
        // Lot Vs Order - Right frame
        //System.out.print ("Reading Order Vs. Lot Status HTML Form screen...");
        String URI_LOT_VS_LOT_STATUS_RT_FRAME = "/online/queryGen.do?BV_SessionID=" + m_observer.getBvSessionId() + "&pmCatOID=-9927&parentOID=-9905&action=init&pagingable=true";
        rsp = con.Get( URI_LOT_VS_LOT_STATUS_RT_FRAME );
        TSMCHtmlParser parser = new TSMCHtmlParser();

        parser.parse( new InputStreamReader( rsp.getInputStream() ) );
        //System.out.println("Token = "+parser.getToken());
        /*while ( rsp.getInputStream().read( data ) != -1 )
        {
            //System.out.println(new String(data));
        }*/

        //System.out.println ("done");

        System.out.println( "\nSubmitting the request for Order Vs Lot status Report ... " );

        NVPair[] formData = new NVPair[54];
        formData[0] = new NVPair( "org.apache.struts.taglib.html.TOKEN", parser.getToken() );
        formData[1] = new NVPair( "reportSetCd", "" );
        formData[2] = new NVPair( "fid", "" );
        formData[3] = new NVPair( "BV_SessionID", m_observer.getBvSessionId() );
        formData[4] = new NVPair( "pmCatOID", "-9927" );
        formData[5] = new NVPair( "turnKeyOID", "" );
        formData[6] = new NVPair( "pagingSize", "20" );
        formData[7] = new NVPair( "surveyID", "17" );
        formData[8] = new NVPair( "isSurveyed", "true" );
        formData[9] = new NVPair( "pmFirstTime", "" );
        formData[10] = new NVPair( "command", "query" );
        formData[11] = new NVPair( "reportSetName", "" );
        formData[12] = new NVPair( "isCustomized", "Y" );
        formData[13] = new NVPair( "linkDesc", "Order vs. Lot Status" );
        formData[14] = new NVPair( "url", "/online/queryGen.do" );
        formData[15] = new NVPair( "pageView", "true" );
        formData[16] = new NVPair( "criterion0", "" );
        formData[17] = new NVPair( "pmSeq", "LIST" );
        formData[18] = new NVPair( "criterias", "Customer Product" );
        formData[19] = new NVPair( "criterion1", "" );
        formData[20] = new NVPair( "pmSeq", "LIST" );
        formData[21] = new NVPair( "criterias", "TSMC Product" );
        formData[22] = new NVPair( "criterion2", "" );
        formData[23] = new NVPair( "pmSeq", "LIST" );
        formData[24] = new NVPair( "criterias", "Customer PO" );
        formData[25] = new NVPair( "criterion3", "" );
        formData[26] = new NVPair( "pmSeq", "LIST" );
        formData[27] = new NVPair( "criterias", "Lot Type" );
        formData[28] = new NVPair( "criterion4", "" );
        formData[29] = new NVPair( "pmSeq", "LIST" );
        formData[30] = new NVPair( "criterias", "Fab" );
        formData[31] = new NVPair( "criterion5", "" );
        formData[32] = new NVPair( "pmSeq", "LIST" );
        formData[33] = new NVPair( "criterias", "Customer Lot ID" );
        formData[34] = new NVPair( "criterion6", "" );
        formData[35] = new NVPair( "pmSeq", "LIST" );
        formData[36] = new NVPair( "criterias", "TSMC Lot ID" );
        formData[37] = new NVPair( "criterion8", "" );
        formData[38] = new NVPair( "pmSeq", "LIST" );
        formData[39] = new NVPair( "criterias", "Number of Days Delayed" );
        formData[40] = new NVPair( "criterion9", "" );
        formData[41] = new NVPair( "pmSeq", "LIST" );
        formData[42] = new NVPair( "criterias", "Number of Days in Current Stage" );
        formData[43] = new NVPair( "criterion10", "" );
        formData[44] = new NVPair( "pmSeq", "LIST" );
        formData[45] = new NVPair( "criterias", "Lot Attribute" );
        formData[46] = new NVPair( "criterion7", m_accountNumber );
        formData[47] = new NVPair( "pmSeq", "LIST" );
        formData[48] = new NVPair( "criterias", "Account" );
        formData[49] = new NVPair( "pagingable", "false" );
        formData[50] = new NVPair( "pmRet", "/online/querygen.do" );
        formData[51] = new NVPair( "onsubmit", "Submit" );
        formData[52] = new NVPair( "HIDDEN_FORM", "true" );

        // -- END--
        rsp = con.Post( "/online/queryGenZZPIResult.do", formData );
        /*while (rsp.getInputStream().read(data) != -1) {
            System.out.println(new String(data));
        }*/

        return rsp;
    }

    /**
     * Logs out from the TSMC Site
     */
    private void logout( HTTPConnection con ) throws Exception
    {
        try
        {
            String logOutURL = URI_LOG_OUT + "?BV_SessionID=" + m_observer.getBvSessionId();
            System.out.print( "\nLoggin out... " );
            HTTPResponse rsp = con.Get( logOutURL );
            System.out.println( "Successfully logged out from TSMC Site" );
            log( "Successfully logged out from TSMC Site" );

            con = null;
            rsp = null;
        }
        catch ( Exception ignore )
        {
            System.out.println( "Exception Occuered While Logging Out From TSMC Site" + ignore );
            throw ignore;
        }
    }

    /**
     * Sends HTTP Post to get Shipping Information
     *
     * @param con connection on which the request is sent for Product Information
     * @return BufferedReader From which the fab data can be read
     *
     * @throws Exception If there is any problem Reading the web pages
     */
    private HTTPResponse requestShippingInfo( HTTPConnection con ) throws Exception
    {

        HTTPResponse httpResponse = null;

        //parameter values specifying the date range for which shipping info is to be retreived
        String shippingInfoDatePeriod = "10";
        Calendar calendar = Calendar.getInstance();
        String shippingInfoEndDate = StringUtils.formatDateToString( calendar.getTime(), "MM/dd/yyyy" );
        calendar.add( Calendar.DATE, -10 );
        String shippingInfoStartDate = StringUtils.formatDateToString( calendar.getTime(), "MM/dd/yyyy" );


        NVPair formData[] = new NVPair[6];

        formData[0] = new NVPair( "BV_SessionID", m_observer.getBvSessionId() );
        formData[1] = new NVPair( "BV_EngineID", m_observer.getBvEngineId() );
        formData[2] = new NVPair( "pmCatOID", CATEGORY_SHIPPING_INFO_QUERY );
        formData[3] = new NVPair( "parentOID", CATEGORY_PARENT_SHIP_INFO_QUERY );
        formData[4] = new NVPair( "action", CATEGORY_ACTION_SHIP_INFO_QUERY );
        formData[5] = new NVPair( "pagingable", PARAM_VALUE_TRUE );
        httpResponse = con.Get( URI_SHIPPING_INFO_QUERY, formData );

         //Parsing the response data for the value of the attribute org.apache.struts.taglib.html.TOKEN
        TSMCHtmlParser parser = new TSMCHtmlParser();
        parser.parse( new InputStreamReader( httpResponse.getInputStream() ) );
        //System.out.println( "Token = " + parser.getToken() );

        System.out.println( "\nSubmitting the request for downloading the shipping information..." );

        formData = new NVPair[48];

        formData[0]     = new NVPair( "org.apache.struts.taglib.html.TOKEN", parser.getToken() );
        formData[1]     = new NVPair( "reportSetCd", "" );
        formData[2]     = new NVPair( "fid", "" );
        formData[3]     = new NVPair( "BV_EngineID", m_observer.getBvEngineId() );
        formData[4]     = new NVPair( "BV_SessionID", m_observer.getBvSessionId() );
        formData[5]     = new NVPair( "pmCatOID", CATEGORY_SHIPPING_INFO_QUERY );
        formData[6]     = new NVPair( "turnKeyOID", "" );
        formData[7]     = new NVPair( "pagingSize", "20" );
        formData[8]     = new NVPair( "surveyID", "10" );
        formData[9]     = new NVPair( "isSurveyed", PARAM_VALUE_TRUE );
        formData[10]    = new NVPair( "pmFirstTime", "" );
        formData[11]    = new NVPair( "command", "query" );
        formData[12]    = new NVPair( "reportSetName", "" );
        formData[13]    = new NVPair( "isCustomized", "Y" );
        formData[14]    = new NVPair( "linkDesc", "Shipping" );
        formData[15]    = new NVPair( "url", "/online/queryGen.do" );
        formData[16]    = new NVPair( "pageView", PARAM_VALUE_TRUE );
        formData[17]    = new NVPair( "criterion0", "" );
        formData[18]    = new NVPair( "pmSeq", "LIST" );
        formData[19]    = new NVPair( "criterias", "Customer Product" );
        formData[20]    = new NVPair( "criterion1", "" );
        formData[21]    = new NVPair( "pmSeq", "LIST" );
        formData[22]    = new NVPair( "criterias", "TSMC Product" );
        formData[23]    = new NVPair( "criterion2", "" );
        formData[24]    = new NVPair( "pmSeq", "LIST" );
        formData[25]    = new NVPair( "criterias", "Customer PO" );
        formData[26]    = new NVPair( "criterion3", "" );
        formData[27]    = new NVPair( "pmSeq", "LIST" );
        formData[28]    = new NVPair( "criterias", "Customer Lot ID" );
        formData[29]    = new NVPair( "criterion4", "" );
        formData[30]    = new NVPair( "pmSeq", "LIST" );
        formData[31]    = new NVPair( "criterias", "TSMC Lot ID" );
        formData[32]    = new NVPair( "dateType", "L" );
        formData[33]    = new NVPair( "datePeriod", shippingInfoDatePeriod );
        formData[34]    = new NVPair( "criterion5", shippingInfoStartDate );
        formData[35]    = new NVPair( "criterion5to", shippingInfoEndDate );
        formData[36]    = new NVPair( "pmSeq", "LIST" );
        formData[37]    = new NVPair( "criterias", "Shipping Date" );
        formData[38]    = new NVPair( "criterion6", m_accountNumber );
        formData[39]    = new NVPair( "pmSeq", "LIST" );
        formData[40]    = new NVPair( "criterias", "Account" );
        formData[41]    = new NVPair( "criterion7", "" );
        formData[42]    = new NVPair( "pmSeq", "LIST" );
        formData[43]    = new NVPair( "criterias", "Ship To" );
        formData[44]    = new NVPair( "pagingable", PARAM_VALUE_TRUE );
        formData[45]    = new NVPair( "pmRet", "/online/querygen.do" );
        formData[46]    = new NVPair( "onsubmit", "Submit" );
        formData[47]    = new NVPair( "HIDDEN_FORM", PARAM_VALUE_TRUE );

        httpResponse = con.Post( URI_SHIPPING_INFO, formData );

        return httpResponse;
    }

    private HTTPResponse requestSortBinInfoData( HTTPConnection con ) throws Exception
    {

        byte[] data = new byte[1024];

        HTTPConnection pcmFilesConnection = new HTTPConnection( PCM_HOST, PORT_NO );

        //HTTPConnection pcmFilesConnection1 = new HTTPConnection( "dsa.tsmc.com", PORT_NO );

        Calendar calendar = Calendar.getInstance();
        String toDate = StringUtils.formatDateToString( calendar.getTime(), "MM/dd/yyyy" );
        calendar.add( Calendar.DATE, -6 );
        String fromDate = StringUtils.formatDateToString( calendar.getTime(), "MM/dd/yyyy" );

        //System.out.println("fromDate = " + fromDate);
        //System.out.println("toDate = " + toDate );

        System.out.print( "\nReading PCM, IQC & CP Result HTML Form screen..." );
                
        ///online/ThirdPage.do?BV_SessionID=0105485005512699682890&pmCatOID=-9912&parentOID=-9911&pagingable=true
        String URI_PCM_DATA_RT_FRAME = "/online/ThirdPage.do?BV_SessionID=" + m_observer.getBvSessionId() + "&pmCatOID=" + CATEGORY_PCM_IQC_CP_RESULTS + "&parentOID=-9911&pagingable=true";

        HTTPResponse rsp = con.Get( URI_PCM_DATA_RT_FRAME );
        /*while ( rsp.getInputStream().read( data ) != -1 )
        {
            //System.out.println(new String(data));
        }*/

        TSMCHtmlParser parser1 = new TSMCHtmlParser();

        parser1.parse( new InputStreamReader( rsp.getInputStream() ) );

        System.out.println( "\tSubmitting the request for PCM Data ... " );


        int datePeriod = 6;

        NVPair[] formData = new NVPair[34];
        //BV_SessionID=0105485007137157416436&pmCatOID=-9912&reportSetCd=&pmCustProdName=&pmTSMCProdName=&dateType=L&datePeriod=30&fromDate=10%2F10%2F2009&toDate=11%2F09%2F2009
        // &pmFabNumber=&pmCustLot=&pmTSMCLot=&pmAccountNumber=U696&pagingable=false&onsubmit=Submit&criterias=Customer+Product&criterias=TSMC+Product&criterias=Order+Entry+Date
        // &criterias=Fab&criterias=Customer+Lot+ID&criterias=TSMC+Lot+ID&criterias=Account&command=QueryResult&pagingSize=20&surveyID=0&isSurveyed=true&reportSetName=&isCustomized=N
        // &linkDesc=PCM%2C+IQC+%26+CP+Result+%28lot+base+file+access%29&url=%2Fonline%2FfileBaseAggrAction.do&pageView=true&HIDDEN_FORM=trueHTTP/1.1 200 OK
        formData[0] = new NVPair( "org.apache.struts.taglib.html.TOKEN", parser1.getToken() );
        formData[1] = new NVPair( "BV_SessionID", m_observer.getBvSessionId() );
        formData[2] = new NVPair( "pmCatOID", CATEGORY_PCM_IQC_CP_RESULTS );
        formData[3] = new NVPair( "reportSetCd", "" );
        formData[4] = new NVPair( "pmCustProdName", "" );
        formData[5] = new NVPair( "pmTSMCProdName", "" );
        formData[6] = new NVPair( "dateType", "L" );
        formData[7] = new NVPair( "datePeriod", datePeriod + "" );
        formData[8] = new NVPair( "fromDate", fromDate );
        formData[9] = new NVPair( "toDate", toDate );
        formData[10] = new NVPair( "pmFabNumber", "" );
        formData[11] = new NVPair( "pmCustLot", "" );
        formData[12] = new NVPair( "pmTSMCLot", "" );
        formData[13] = new NVPair( "pmAccountNumber", m_accountNumber );
        formData[14] = new NVPair( "pagingable", "false" );
        formData[15] = new NVPair( "onsubmit", "Submit" );
        formData[16] = new NVPair( "criterias", "Customer Product" );
        formData[17] = new NVPair( "criterias", "TSMC Product" );
        formData[18] = new NVPair( "criterias", "Order Entry Date" );
        formData[19] = new NVPair( "criterias", "Fab" );
        formData[20] = new NVPair( "criterias", "Customer Lot ID" );
        formData[21] = new NVPair( "criterias", "TSMC Lot ID" );
        formData[22] = new NVPair( "criterias", "Account" );
        formData[23] = new NVPair( "command", "QueryResult" );
        formData[24] = new NVPair( "pagingSize", "20" );
        formData[25] = new NVPair( "surveyID", "0" );
        formData[26] = new NVPair( "isSurveyed", "true" );
        formData[27] = new NVPair( "reportSetName", "" );
        formData[28] = new NVPair( "isCustomized", "N" );
        formData[29] = new NVPair( "linkDesc", "PCM, IQC & CP Result (lot base file access)" );
        formData[30] = new NVPair( "url", "/online/fileBaseAggrAction.do" );
        formData[31] = new NVPair( "pageView", "true" );
        formData[32] = new NVPair( "HIDDEN_FORM", "true" );

        rsp = con.Post( "/online/fileBaseAggrAction.do", formData );
        System.out.println( "done\n" );

       
        ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 );
        PrintWriter printWriter = new PrintWriter( baos );
        BufferedReader pcmDataResultsPageReader = new BufferedReader( new InputStreamReader( rsp.getInputStream() ) );
        String line = null;

        while ( ( line = pcmDataResultsPageReader.readLine() ) != null )
        {
            if ( line.indexOf( "</BODY>" ) == -1 )
            {
                printWriter.println( line );
            }
        }

        printWriter.close();
        //System.out.println(new String(baos.toByteArray()));
        
        ArrayList pcmDataList = new TSMCHtmlParser( m_observer.getBvSessionId() )
                .parsePCM( new InputStreamReader( new ByteArrayInputStream( baos.toByteArray() ) ) );
        //System.out.println ("done\n");


        TSMCLotStatus pcmData = null;
        String binFileLink = null;
        String lotNumber   = null;
        int pcmDataListSize = pcmDataList.size();

        System.out.println( "No of PCM Data files trying to download : " + pcmDataListSize );
        log( "No of PCM Data files trying to download  : " + pcmDataListSize );
        for ( int i = 0; i < pcmDataListSize; i++ )
        {
            pcmData = ( TSMCLotStatus ) pcmDataList.get( i );

            //binFileName = pcmData.getBinFileName();
             lotNumber   = pcmData.getCustomerLotID();
             binFileLink  = pcmData.getBinFileHyperLink();//WPR FILE
            // The TSMC website is transforing the link to another website.
            // And there it is giving unauthorised user access pages.
            
            if ( ( binFileLink != null ) && ( binFileLink.trim().length() != 0 ) )
            {
                log( "Downloading BIN file for Lot Number: " + lotNumber + "..." );
                System.out.println( "Downloading BIN file for Lot Number: " + lotNumber + "..." );
                                
                ///cgi-bin/vm225901.bin?pmFileType=2&pmFile=/e/bin/vis/u696/vm225901.bin

                //rsp = pcmFilesConnection.Get("/cgi-bin/"+binFileName.substring( binFileName.lastIndexOf("/") + 1 , binFileName.lastIndexOf(".") ) +".wpr?pmFileType=2&pmFile=" +binFileName);

                rsp = pcmFilesConnection.Get( pcmData.getBinFileHyperLink() );
                                

                AdaptorUtils.saveAsFile( new BufferedReader( new InputStreamReader( rsp.getInputStream() ) ), m_dataOutputDir, BINING_FILE_NAME+ "_" + lotNumber, FILE_EXTN_TXT, true /* append time stamp */ );


                if ( m_adaptor_id > 0 )
                {
                    AdaptorRunInfo.saveAdaptorFileInfo( m_adaptor_id , BINING_FILE_NAME+ "_" + lotNumber, FILE_EXTN_CSV, StringUtils.formatDateandTime(DateUtil.getFormattedTimeStamp() )) ;
                }
            }
        }
        System.out.println( ".....done" );
        log( ".......done" );
        return rsp;
    }

    /**
     * Scans for Download link and Sends HTTP Request to the download link and returns the Reader
     *
     * @param rsp Response object which is scanned for the download link
     * @return BufferedReader To read the data
     *
     * @throws Exception If there are any problems while sending HTTP request
     */
    private BufferedReader getReader( HTTPResponse rsp ) throws Exception
    {

        byte[] data = new byte[1024];
        BufferedReader reader = null;

        System.out.println( "\n\tScanning for Download link ...... " );
        HtmlScanner htmlScanner = new HtmlScanner( rsp.getInputStream(), s_dummyUrl, m_observer );
        while ( htmlScanner.read( data ) != -1 )
        {
            //System.out.println (new String(data));
        }
                
        String dlLink = m_observer.getDownloadCsvURL();
        if ( dlLink == null )
        {
            m_observer.getDownloadTextURL();
            rsp = null;
        }
        if ( rsp == null )
        {
            System.out.println( "\n****  No DownLoad Links are Exists, (No Shipping Records Found) ****\n " );
            return reader;
        }

        System.out.print( "Downloading data from...." /*+ dlLink + "... "*/ );
        URL downloadLink = new URL( dlLink );

        int port = downloadLink.getPort();
        port = ( ( port == -1 ) ? 80 : port );
        HTTPConnection con = new HTTPConnection( downloadLink.getHost(), port );

        rsp = con.Get( downloadLink.getFile() );
        reader = new BufferedReader( new InputStreamReader( rsp.getInputStream() ) );

        return reader;
    }

    private void log( String message )
    {

        if ( message == null ) return;

        if ( m_adaptorLogger != null )
        {
            synchronized ( m_adaptorLogger )
            {
                m_adaptorLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }
}
